﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Traceable.Domian;

namespace Traceable.Infastructure
{
    public class BaseRepository<T> : IBaseRepository<T> where T : BaseEntity
    {
        TraceableDbContext _db;

        public BaseRepository(TraceableDbContext db)
        {
            _db = db;
        }
        //新增
        public int Add(T entity)
        {
            _db.Set<T>().Add(entity);
            return _db.SaveChanges();
        }
        //删除
        public int Delete(T entity)
        {
            _db.Set<T>().Remove(entity);
            return _db.SaveChanges();
        }
        //批量删除
        public int DeleteRange(IEnumerable<T> entities)
        {
            _db.Set<T>().RemoveRange(entities);
            return _db.SaveChanges();
        }
        //查询
        public IQueryable<T> GetAll()
        {
            return _db.Set<T>().AsQueryable();
        }
        //表达式查询
        public T GetValue(long id)
        {
            return _db.Set<T>().Find(id);
        }
        //修改
        public int Update(T entity)
        {
            _db.Set<T>().Update(entity);
            return _db.SaveChanges();
        }
        //批量修改
        public int UpdateRange(IEnumerable<T> entities)
        {
            _db.Set<T>().UpdateRange(entities);
            return _db.SaveChanges();
        }
    }
}
